<template>
<view class="container">
<form-detail-component id="taskData" showTitle='false' :showSubmitPopup="false" :showBtn="false" :receiveData="sendOptionData" :locationList2="locationList" @selectPsotion="selectPsotion" v-if="sendPageDataState" @sendDataFun="getDataFun"></form-detail-component>
<!-- <view class='shareFormPoster' style='background:{{setting.platformSetting.defaultColor}}' bindtap='showPoster'>
  <image class='shareFormPosterImg' src='https://image1.sansancloud.com/xianhua/2018_12/26/11/57/10_263.jpg'></image>
</view> -->
<form-chain-poster-component :formId="formId" :ewmImgUrl="ewmImgUrl" v-if="posterState" @closePoaster="getChilrenPoster"></form-chain-poster-component>
 <!-- 首页组件-详情装饰-->
 <!-- <custom-page-index-component :receiveData="sendFormData" v-if="sendFormData"></custom-page-index-component> -->
 <view class="create_task_btn_block">
     <view class="create_task_btn_left" v-if="taskTypeDetailData.defaultTaskPrice>0&&taskTypeDetailData.priceMode!=0">
         <text class="">市场参考价：</text>
         <text :style="{color:setting.platformSetting.defaultColor}" class="task_price">￥{{taskTypeDetailData.defaultTaskPrice}}</text>
     </view>
     <view class="create_task_btn_right" :style="{background:setting.platformSetting.defaultColor,width:((taskTypeDetailData.defaultTaskPrice>0&&taskTypeDetailData.priceMode!=0)?'30%':'100%')}" @click="submitTaskFun()">
         <text class="create_task_btn_right_text">确认提交</text>
     </view>
 </view>
</view>
</template>

<script>

export default {
  data() {
    return {
      formChainData: null,
      curCommitformId: '',
      formData: null,
      processType: false,
      refProductFormType: false,
      productId: 0,
      formId: 0,
      taskTypeId: 0,
      reqLocation: false,
      locationList: {},
      locationIndex: "",
      sendOptionData: null,
      sendPageDataState: false,
      sendFormData: null,
      params: {
        formJson: '',
        customFormId: '',
        miniNotifyFormId: ''
      },
      posterState: false,
      ewmImgUrl: "",
      setting: "",
      loginUser: "",
      taskTypeDetailData:null,
    };
  },

  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log(options);
    let sendFormData = JSON.stringify({
      title: 'noTitle',
      url: "form_chain_" + options.taskTypeId
    });
    that.setData({
      sendFormData: sendFormData
    });
    if (options.taskTypeId) {
        that.params.taskTypeId = options.taskTypeId;
        that.setData({
          taskTypeId: options.taskTypeId
        });
    }
    that.getTaskTypeDetailFun(options.taskTypeId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      setting: getApp().globalData.setting
    });
    this.setData({
      loginUser: getApp().globalData.loginUser
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if (that.reqLocation) {
      // that.setData({ sendPageDataState: false })
      // that.setData({ sendPageDataState: true })
      let locationList = {};
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1]; //当前页面

      console.log(currPage); //就可以看到data里mydata的值了
      that.selectAddress=currPage.data.selectAddress
      console.log("从选择地点页面返回", that.selectAddress);
      if (that.selectAddress) {
        console.log("选择了地点");
        console.log("locationIndex", that.locationIndex);
        locationList[that.locationIndex] = that.selectAddress;
        that.locationList = Object.assign({}, that.locationList, locationList);
        that.setData({
          locationList: that.locationList
        });
        console.log("==locationList==", locationList);
        console.log("==that.data.locationList ==", that.locationList);
      } else {
        console.log("没选择地点");
      }
      that.selectComponent("#taskData").selectAddress(that.locationList);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  methods: {
    selectPsotion:function(e){
      let that = this;
      console.log("===selectPsotion===", e);
      let locationIndex = e.detail.locationIndex;
      that.setData({
        reqLocation: true,
        locationIndex: locationIndex
      });
    },
    // 关闭海报
    getChilrenPoster(e) {
      let that = this;
      that.setData({
        posterState: false
      });
    },
    showPoster: function () {
      let that = this;
      console.log('===showPoster====', that.params.customFormId);
      let ewmImgUrl = getApp().globalData.getQrCode({
        type: "form_detail",
        id: that.params.customFormId
      });
      that.setData({
        posterState: true,
        ewmImgUrl: ewmImgUrl
      });
    },
    tolinkUrl: function (e) {
      if (!getApp().globalData.loginUser) {
        wx.showModal({
          title: '提示',
          content: '主人~您还在登陆哦!稍等片刻',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
            } else if (res.cancel) {
              console.log('用户点击取消');
            }
          }
        });
        return;
      }

      let that = this;
      let linkUrl = e.currentTarget ? e.currentTarget.dataset.link : e;
      let index = e.currentTarget ? e.currentTarget.dataset.index : 0;

      if (linkUrl.indexOf("select_location.html") != -1) {
        console.log("选择位置");
        that.setData({
          reqLocation: true,
          locationIndex: "position_" + index
        });
      } else {
        that.setData({
          reqLocation: false
        });
      }

      getApp().globalData.linkEvent(linkUrl);
    },
    getTaskTypeDetailFun:function(id){
        let that = this;
        console.log("===getTaskTypeDetailFun===", id);
        let params = {
          taskTypeId: id|| 0,
        };
        let taskTypeDataUrl = getApp().globalData.AddClientUrl("/wx_get_task_type_detail.html", params, 'get');
        wx.request({
          url: taskTypeDataUrl.url,
          data: taskTypeDataUrl.params,
          header: getApp().globalData.headerPost,
          method: 'get',
          success: function (res) {
            console.log(res);
            let taskTypeDetailData=res.data.relateObj
            if(res.data.errcode==-1){
                taskTypeDetailData=null
            }else{
                wx.setNavigationBarTitle({
                  title: taskTypeDetailData.name
                });
                let sendOptionData = {
                  customFormId: taskTypeDetailData.moreInfoCustomFormId
                };
                that.setData({
                  sendPageDataState: false,
                  sendOptionData: sendOptionData,
                });
                setTimeout(function(){
                    that.setData({sendPageDataState: true})
                },500)
            }
            that.setData({
              taskTypeDetailData: taskTypeDetailData
            });
          },
        })
    },
    submitTaskFun:function(){
        let that=this;
        that.selectComponent("#taskData").formSubmit();
    },
    getDataFun: function (e) {
      let that = this;
      console.log("===getDataFun===", e);
      that.createPlatformUserTaskFun(e.detail.formId)
    },
    createPlatformUserTaskFun: function (formCommitId) {
      let that = this;
      let taskTypeDetailData=that.taskTypeDetailData
      console.log("===createPlatformUserTaskFun===", formCommitId);
      let params = {
        formCommitId: formCommitId || 0,
        customFormId: taskTypeDetailData.moreInfoCustomFormId,
        taskTypeId:taskTypeDetailData.id
      };
      let createTaskDataUrl = getApp().globalData.AddClientUrl("/wx_create_platform_user_task.html", params, 'get');
      wx.request({
        url: createTaskDataUrl.url,
        data: createTaskDataUrl.params,
        header: getApp().globalData.headerPost,
        method: 'get',
        success: function (res) {
          console.log(res);
          let url='task_user_list.html'
          that.tolinkUrl(url)
        }
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>